// 将数组按照VueRouter格式转成树
export const ArrayFormatVueRouter = (
  array,
  pid = null,
  parentStorey = null
) => {
  return array.reduce((prev, cur) => {
    if (Number(cur.meta.pid) == Number(pid)) {
      let storey = parentStorey ?? 0;
      let meta = cur.meta;
      cur.meta.storey = storey;
      if (meta.type == 1) {
        let component = cur.meta.component;
        if (component) {
          cur.component = () => import(`@/pages/${cur.meta.component}.vue`);
        } else {
          cur.component = () =>
            import(`@/pages/layout/components/router-view/router-view.vue`);
        }
      } else {
        cur.component = () => import(`@/pages/${cur.meta.component}.vue`);
      }
      let {
        // redirect_name,
        redirect_state,
        // redirect_target,
        redirect_type,
        redirect_url,
      } = cur.meta.redirect;

      if (redirect_state && redirect_type && redirect_url) {
        cur.redirect = redirect_url;
      }
      const children = ArrayFormatVueRouter(array, meta.id, ++storey);
      cur.children = children;
      prev.push(cur);
    }
    return prev;
  }, []);
};
// 根据child的ID 寻找祖先ID
export const childIdGeTAncestorsId = (array, oid = null) => {
  let orn = array.find((item) => item.meta.id == oid);
  let { pid } = orn.meta;
  if (pid) {
    return childIdGeTAncestorsId(array, pid);
  } else {
    return orn.meta.id
  }
};

// 格式化数据
export const formatMenuItem = (item) => {
  let { name = "", path = "", redirect = null, component = null } = item;
  let meta = item.meta;
  let display = meta?.display ?? {};
  let componentFilePath = meta.component
    ? `@/pages/${meta.component}.vue`
    : null;
  return {
    name,
    path,
    redirect,
    component,
    meta: {
      id: meta.id,
      pid: meta.pid ?? null,
      title: meta.title ?? "",
      icon: meta.icon ?? "",
      component: meta.component,
      componentFilePath,
      storey: 0, // 菜单层级
      display: {
        // 显示
        ifMenuBar: display?.ifMenuBar ?? false, // 是否为菜单
        breadcrumb: display?.breadcrumb ?? true, // 面包屑显示
        navigator: display?.navigator ?? true, // 导航菜单显示
        navigatorRightIcon: display?.navigatorRightIcon ?? false, // 菜单右侧箭头图标
        normalIconClass: meta.icon ?? "", // 正常状态菜单图标
        hoverIconClass: meta.icon ?? "", // hover状态菜单图标
        activedChangeId: null, // 当菜单激活时,将激活的ID指向这个ID，用于高亮其他ID的菜单
        childrenDevelop: display?.childrenDevelop ?? false,
      },
      sortIndex:null, // 排序
      iconNormalName: null, // 路由正常时图标样式
      iconActiveName: null, // 路由激活时图标样式
      rightArrowShow: null,// 路由右侧箭头显示状态
      activeFocusPath: null, // 激活菜单时将焦点聚焦到该路由下，而非当前路由
      navigatorShow:null, // 是否在侧边栏显示
      comnent:"" // 路由说明
    },
  };
};
export default {
  childIdGeTAncestorsId,
  ArrayFormatVueRouter,
  formatMenuItem,
};
